package com.example.smart.ui.m.frag

import android.content.Context
import android.view.View
import androidx.core.widget.NestedScrollView
import com.example.lib.extend.gridManager
import com.example.lib.extend.horizontalManager
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.sp.SpKey
import com.example.lib.utils.sp.SpUtils
import com.example.smart.R
import com.example.smart.adapter.home.MBannerAdapter
import com.example.smart.adapter.home.MMenuAdapter
import com.example.smart.adapter.home.MPreventionAdapter
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.ui.fragment.BaseFragment
import com.example.smart.data.MenuData
import com.example.smart.data.PreventionData
import com.example.smart.databinding.FragHomeMBinding
import com.example.smart.dialog.AddressDialog
import com.example.smart.listener.OnChildScrollListener
import com.example.smart.utils.DisplayUtils

class MHomeFragment : BaseFragment<FragHomeMBinding>() {
    private var mScrollListener: OnChildScrollListener? = null

    private val mAddressDialog by lazy {
        activity?.let {
            AddressDialog(it).also { dialog ->
                dialog.onResult = { result ->
                    binding.LocationText.text = result
                }
            }
        }
    }

    private val mBannerAdapter by lazy {
        activity?.let {
            MBannerAdapter(it)
        }
    }

    private val mMenuAdapter by lazy {
        activity?.let {
            MMenuAdapter(it)
        }
    }

    private val mVideoAdapter by lazy {
        activity?.let {
            MPreventionAdapter(it)
        }
    }

    companion object {
        fun getInstance(): MHomeFragment {
            return MHomeFragment()
        }
    }

    override fun onAttach(context: Context) {
        if (context is OnChildScrollListener) {
            mScrollListener = context
        }
        super.onAttach(context)
    }

    override fun initView(view: View) {
        binding.HomeBanner.horizontalManager()
        binding.HomeBanner.adapter = mBannerAdapter

        binding.HomeMenu.gridManager(4)
        binding.HomeMenu.adapter = mMenuAdapter
        binding.HomeMenu.isNestedScrollingEnabled = false

        binding.HomeVideo.horizontalManager()
        binding.HomeVideo.adapter = mVideoAdapter

        val menuData = mutableListOf<MenuData>()
        menuData.add(MenuData(1, R.mipmap.icon_menu1, getString(R.string.home_menu1), R.mipmap.m_home_menu1))
        menuData.add(MenuData(2, R.mipmap.icon_menu2, getString(R.string.home_menu2), R.mipmap.m_home_menu2))
        menuData.add(MenuData(3, R.mipmap.icon_menu3, getString(R.string.home_menu3), R.mipmap.m_home_menu3))
        menuData.add(MenuData(4, R.mipmap.icon_menu4, getString(R.string.home_menu4), R.mipmap.m_home_menu4))
        menuData.add(MenuData(5, R.mipmap.icon_menu5, getString(R.string.home_menu5), R.mipmap.m_home_menu5))
        menuData.add(MenuData(6, R.mipmap.icon_menu6, getString(R.string.home_menu6), R.mipmap.m_home_menu6))
        menuData.add(MenuData(7, R.mipmap.icon_menu7, getString(R.string.home_menu7), R.mipmap.m_home_menu7))
        menuData.add(MenuData(8, R.mipmap.icon_menu8, getString(R.string.home_menu8), R.mipmap.m_home_menu8))
        mMenuAdapter?.updateList(menuData)
        binding.LocationText.text = SpUtils.getString(SpKey.ADDRESS, getString(R.string.default_address))

        val videoData = mutableListOf<PreventionData>()
        videoData.add(PreventionData(R.mipmap.icon_video1,
            getString(R.string.home_video_title1),
            getString(R.string.home_video_message1)))
        videoData.add(PreventionData(R.mipmap.icon_video2,
            getString(R.string.home_video_title2),
            getString(R.string.home_video_message2)))
        mVideoAdapter?.updateList(videoData)

        binding.ImageNotice.setUnReaderCount("2")
    }

    override fun initListener() {
        binding.HomeSearch.setOnDoubleClickListener {
            DisplayUtils.toSearch(activity)
        }

        binding.ImageNotice.setOnDoubleClickListener {
            DisplayUtils.toChatList(activity)
        }

        binding.ScrollView.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, _, _, _ ->
            if (binding.ScrollView.canScrollVertically(-1)) {
                mScrollListener?.onScroll(true)
            } else {
                mScrollListener?.onScroll(false)
            }
        })

        binding.UserPhoto.setOnDoubleClickListener {
            mScrollListener?.openMenu()
        }

        binding.LocationView.setOnDoubleClickListener {
            mAddressDialog?.show()
        }

        mMenuAdapter?.setOnClickListener(object : BaseRecyclerAdapter.OnClickListener<MenuData> {
            override fun click(bean: MenuData, position: Int) {
                when (bean.id) {
                    1 -> {//每日一测
                        DisplayUtils.toEverydayTest(activity)
                    }

                    2 -> {
                        DisplayUtils.toJz(activity)
                    }

                    3 -> {//防疫证明
                        DisplayUtils.toSheepList(activity, 1)
                    }

                    4 -> {
                        DisplayUtils.toSheepList(activity, 2)
                    }

                    5 -> {//数据检测
                        DisplayUtils.toMine(activity)
                    }

                    6 -> {//哪里有苗
                        DisplayUtils.toVaccinesList(activity)
                    }

                    7 -> {//养殖精灵
                        DisplayUtils.toAiConversation(activity)
                    }

                    8 -> {
                        ToastUtils.showToast("暂未开发")
                    }
                }
            }
        })

        binding.HomeViewAll.setOnDoubleClickListener {
            DisplayUtils.toPrevention(activity)
        }
    }

}